#!/usr/bin/bash

CFLAGS="-Wall -Wextra -Wpedantic 
    -Wmissing-declarations -Wstrict-prototypes -Wconversion 
    -Wno-missing-declarations -std=c23 -g3 -lm -O2
    $CFLAGS"
CC="${CC:-gcc}"
TARGET=(spacman pacupd)

set -e
get_help() {
        echo "Usage: $0 [options] (install \$HOME)"
        echo -e "  -r, --release, release, r\tAdd \`-static\` and not add \`-march=native\` in build."
        echo -e "  -m, --makepkg, makepkg, m\tUse makepkg to build then package."
        echo -e "  -i, --install, install, i\tInstall to \`\$HOME/.local/bin/\`. (\$HOME must be given)"
        echo -e "  -c, --clean, clean, c\tClean up. (Will not uninstall the software)"
        echo -e "  -h, --help, help h\tShow this message."
        echo
        echo "Here is some example"
        echo -e "\t$0"
        echo -e "\t\t""Build all the target."
        echo -e "\t$0 makepkg spacman"
        echo -e "\t\t""Make 'spacman' package"
        echo -e "\t$0 install \$HOME pacupd"
        echo -e "\t\tInstall 'pacupd' to your home."
        echo -e "\t$0 clean"
        echo -e "\t\tClean up *pkg* file and the target file"
        echo
        echo "Good Luck with that"
}

# After task
install_task='n'
makepkg_task='n'
# Build what
spacman_task='n'
pacupd_task='n'
special_target='n'
# Super action
clean_task='n'
version_task='n'
help_task='n'
# Configure
home_path=''
release_task='n'
# Statue
final_ok='y'

while [ "$#" != 0 ]; do
        case "$1" in
        spacman)
                special_target='y'
                spacman_task='y'
        ;;pacupd)
                special_target='y'
                pacupd_task='y'
        ;;-r|r|release|--release)
                release_task='y'
        ;;-m|m|makepkg|--makepkg)
                makepkg_task='y'
        ;;-i|i|install|--install)
                install_task='y'
        ;;-c|c|clean|--clean)
                clean_task='y'
        ;;-h|h|help|--help)
                help_task='y'
        ;;-V|-v|--version|version)
                version_task='y'
        ;;/home/*)
                if [ "$home_path" = '' ]; then
                        home_path="$1"
                else
                        echo "Path given duper!" >&2
                        exit 1
                fi
        ;;*)
                echo "Fail to read \`$1\`" >&2
                echo "" >&2
                get_help
                exit 1
        esac
        shift
done

if [ $help_task = y ]; then
        get_help
        exit 0
fi

if [ $version_task = y ]; then
        echo "LOL, I dont support version."
        echo "Well, maybe 0.2.0"
        exit 0
fi

if [ $clean_task = y ]; then
        if [ $special_target = y ]; then
                echo "You set build target and clean task at the same time" >&2
                echo "See help" >&2
                exit 1
        else
                rm -f "pacupd/pacupd" "spacman/spacman" \
                    pacupd/*.pkg* spacman/*.pkg*
                rm -rf "spacman/pkg/" "spacman/src/" \
                    "pacupd/pkg/" "pacupd/src/"
                exit 0
        fi
fi

if [ $special_target = 'y' ]; then
        TARGET=()
        if [ $spacman_task = y ]; then
                TARGET+=("spacman")
        fi
        if [ $pacupd_task = y ]; then
                TARGET+=("pacupd")
        fi
fi

if [ $release_task = y ]; then
        export CFLAGS="$CFLAGS -static"
else
        export CFLAGS="$CFLAGS -march=native"
fi

if [ $install_task = y ]; then
        if [ "$home_path" = '' ]; then
                echo "No home given!" >&2
                exit 1
        else
                home_path="$(realpath "$home_path")"
                INST="$home_path/.local/bin"
                read -p "Install to \`$INST/\`?[y/N]: " GETANS
                if [[ "$GETANS" =~ ^[yY]$ ]]; then
                        if [ ! -d "$INST" ]; then
                                mkdir -p "${INST}/"
                                user_name="${home_path#/home/}"
                                chown "$username":"$username" "$INST/../"
                                chown "$username":"$username" "${home_path#/home/}" "$INST/"
                                echo "Make sure \`$INST/\` is in your \`\$PATH\`"
                        fi
                else
                        echo "User give up"
                        exit 1
                fi
        fi
fi

build_target() {
        cd "$1"
        "$CC" $CFLAGS "$1.c" -o "$1"
        if [ $makepkg_task = y ]; then
                # makepkg -c
                makepkg
        fi
        if [ $install_task = y ]; then
                cp "$1" "$INST"
                chown root:root "$INST/$1"
                chmod u+s "$INST/$1"
        fi
        cd ..
}

for target in "${TARGET[@]}"; do
        build_target $target
done
